package cn.com.fuhuitong.main.goods.adpter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import cn.com.fuhuitong.main.goods.entity.GoodsDetailsEntity
import cn.com.fuhuitong.main.goods.entity.GoodsListEntity
import cn.com.fuhuitong.main.goods.entity.GoodsSpecsEntity
import cn.com.fuhuitong.utils.SpanUtils
import cn.com.fuhuitong.utils.WebViewUtils
import com.alibaba.android.vlayout.LayoutHelper
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_web_view.*
import kotlinx.android.synthetic.main.include_goods_content.view.*
import kotlinx.android.synthetic.main.include_goods_details.view.*
import kotlinx.android.synthetic.main.include_goods_heads.view.*
import kotlinx.android.synthetic.main.item_goods_details_banner.view.*
import kotlinx.android.synthetic.main.item_goods_details_bottom.view.*
import kotlinx.android.synthetic.main.item_goods_details_label.view.*
import kotlinx.android.synthetic.main.item_goods_list.view.*
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements


enum class GVT(value: Int) {
    GOODS_LIST(1),
    GOODS_DETAILS(2),
    GOODS_CONTENT(3),
    GOODS_BOTTOM(4)
}

class GoodsListAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<GoodsListEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_goods_list,
    mItemViewType = GVT.GOODS_LIST.ordinal
) {
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        val goodsListEntity: GoodsListEntity = data[position]

        Glide.with(itemView).load(getImageUrl(goodsListEntity.image))
            .into(itemView.image_goods_icon)
        itemView.text_goods_name.text = goodsListEntity.title
        itemView.text_goods_price.text = SpanUtils.setMoneyStyle(goodsListEntity.price.toDouble())
        itemView.text_goods_sales_volume.text = "销量：${goodsListEntity.sales_volume}"
    }
}

class GoodsContentAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<GoodsDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_goods_details_content,
    mItemViewType = GVT.GOODS_CONTENT.ordinal
) {

    init {
        addChildClickViewIds(R.id.text_goods_details_specs, R.id.text_goods_details_group_name)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(itemView: View, position: Int, itemData: GoodsDetailsEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        itemView.text_goods_details_name.text = itemData.title
        itemView.text_goods_details_price.text = SpanUtils.setMoneyStyle(itemData.price.toDouble())
        itemView.text_goods_details_sales_volume.text = "销量：${itemData.sales_volume}"
        itemView.text_goods_details_group_name.text = if (itemData.group_name.isNotEmpty()) {
            itemData.group_name
        } else ""
        itemView.text_goods_details_group_name.visibility = if (itemData.group_name.isNotEmpty()) {
            View.VISIBLE
        } else View.GONE

        val specsEntity: GoodsSpecsEntity? = itemData.specifications
        if (specsEntity == null) {
            itemView.text_goods_details_specs.text = "请选择：规格/数量"
        } else {
            itemView.text_goods_details_specs.text =
                "已选择：${specsEntity.specifications_name}${if (specsEntity.specifications_name.isEmpty()) "" else "，"}${specsEntity.count}件"
        }

        val banners: List<String> = itemData.banner_images.split(",")
        with(itemView.view_pager_goods_details_banner) {
            adapter = DetailsBannerAdapter(mContext, banners)
        }
        itemView.view_pager_goods_details_banner.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                itemView.text_goods_details_banner_num.text = (position + 1).toString()
            }
        })
        itemView.text_goods_details_banner_num.text = "1"
        itemView.text_goods_details_banner_count.text = banners.size.toString()


        val layoutManager = GridLayoutManager(mContext, 4)
        with(itemView.recycler_goods_details_label) {
            this.layoutManager = layoutManager
            this.adapter = DetailsLabelAdapter(mContext, itemData.label_name)
        }
        itemView.web_goods_details_content.clearCache(true)
        WebViewUtils.loadFullScreenHtml(itemView.web_goods_details_content, itemData.content)
    }

    //
    class DetailsBannerAdapter(private val mContext: Context, private val banners: List<String>) :
        RecyclerView.Adapter<RecyclerViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_goods_details_banner, parent, (false))
            )
        }

        override fun getItemCount(): Int {
            return banners.size
        }

        override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
            val itemView: View = holder.itemView
            Glide.with(itemView).load(getImageUrl(banners[position]))
                .into(itemView.text_goods_details_banner_icon)
        }

    }

    class DetailsLabelAdapter(private val mContext: Context, private val labelName: List<String>) :
        RecyclerView.Adapter<RecyclerViewHolder>() {

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): RecyclerViewHolder {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_goods_details_label, parent, (false))
            )
        }

        override fun getItemCount(): Int {
            return labelName.size
        }

        override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
            with(holder.itemView.text_goods_details_label_name) {
                text = labelName[position]
            }
        }
    }
}

class GoodsBottomAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<GoodsDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_goods_details_bottom,
    mItemViewType = GVT.GOODS_BOTTOM.ordinal
) {

    var isMember: Boolean = false

    init {
        addChildClickViewIds(
            R.id.text_goods_details_service,
            R.id.text_goods_details_cart,
            R.id.text_goods_details_add_cart,
            R.id.text_goods_details_buy_now
        )
    }

    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        itemView.text_goods_details_add_cart.visibility =
            if (isMember) View.GONE else View.VISIBLE
        itemView.text_goods_details_buy_now.text =
            if (isMember) "立即预约" else "立即购买"
    }
}